package com.xiaoyilin.algorithm;

import io.shardingjdbc.core.api.algorithm.sharding.PreciseShardingValue;
import io.shardingjdbc.core.api.algorithm.sharding.RangeShardingValue;
import io.shardingjdbc.core.api.algorithm.sharding.standard.PreciseShardingAlgorithm;
import io.shardingjdbc.core.api.algorithm.sharding.standard.RangeShardingAlgorithm;
import lombok.extern.slf4j.Slf4j;
import java.util.Collection;

/**
 * @author xiaoyilin
 * 这里为什么要 %2
 * 	因为现在存在2个数据库，而不是数据表的数量
 */
@Slf4j
public class TradeRecordSingleKeyDatabaseShardingAlgorithm implements PreciseShardingAlgorithm<Long>, RangeShardingAlgorithm<Long> {

    @Override
    public String doSharding(Collection<String> collection, PreciseShardingValue<Long> preciseShardingValue) {
        for (String each : collection) {
            Long businessid = preciseShardingValue.getValue();
            if (each.endsWith(businessid % 2 + "")) {
                log.debug("the target database name: {}", each);
                return each;
            }
        }
        throw new IllegalArgumentException();
    }

    @Override
    public Collection<String> doSharding(Collection<String> collection, RangeShardingValue<Long> rangeShardingValue) {
        return null;
    }


}